//
//  HomeConfirmOrderController.m
//  RentTable
//
//  Created by mac on 2024/11/30.
//

#import "HomeConfirmOrderController.h"
#import "RecipInfoView.h"
#import "OrderInfoView.h"
#import "OrderView.h"
#import "RentRuleView.h"
#import "HomeOrderReviewController.h"

@interface HomeConfirmOrderController ()

@property (nonatomic, strong) UIView *orderbackView;
@property (nonatomic, strong) OrderView *orderView;
@property (nonatomic, strong) OrderInfoView *orderInfoView;

@property (nonatomic, strong) UIView *orderRecipbackView;
@property (nonatomic, strong) RecipInfoView *orderRecipView;
@property (nonatomic, strong) RentRuleView *orderRuleView;

@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UIButton *tenantButton;

@end

@implementation HomeConfirmOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadUI];
    
}


- (void)loadUI {
    self.view.backgroundColor = myRGBHex(0xF8F8F8);
    [self loadBackArrow];
    [self loadTitle:@"确认订单"];
    
    self.orderbackView = [UIView new];
    self.orderbackView.backgroundColor = myRGBHex(0xFFFFFF);
    self.orderbackView.layer.cornerRadius = 20;
    self.orderbackView.layer.masksToBounds = true;
    [self.view addSubview:self.orderbackView];
    [self.orderbackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(104 * scaleH);
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.height.mas_equalTo(280 * scaleH);
    }];
    
    self.orderView = [OrderView new];
    self.orderView.rentModel = self.rentModel;
    [self.orderbackView addSubview:self.orderView];
    [self.orderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.mas_equalTo(0);
        make.height.mas_equalTo(120 * scaleH);
    }];
    
    UIView *devidView = [UIView new];
    devidView.backgroundColor = myRGBHex(0xCCCCCC);
    [self.orderbackView addSubview:devidView];
    [devidView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderView.mas_bottom).offset(5);
        make.leading.mas_equalTo(14);
        make.trailing.mas_equalTo(-14);
        make.height.mas_equalTo(0.5);
    }];
    
    NSString *ordernumber = [NSString randomNumberStringWithLength:18];
    NSString *orderTime = [NSString currentDateString];
    NSString *orderstarttime = [NSString currentOrderDateString];
    
    self.rentModel.rentOrderNumber = ordernumber;
    self.rentModel.rentOrderTime = orderTime;
    self.rentModel.rentStartTime = orderstarttime;
    
    NSDictionary *orderInfo = @{
        @"订单编号" : ordernumber,
        @"下单时间" : orderTime,
        @"起租时间" : orderstarttime,
        @"归还时间" : @"待定"
    };
    self.orderInfoView = [[OrderInfoView alloc] initWithFrame:CGRectZero orderInfo:orderInfo];
    [self.orderbackView addSubview:self.orderInfoView];
    [self.orderInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(devidView.mas_bottom).offset(2);
        make.leading.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(-3);
    }];
    
    self.orderRecipbackView = [UIView new];
    self.orderRecipbackView.backgroundColor = myRGBHex(0xFFFFFF);
    self.orderRecipbackView.layer.cornerRadius = 20;
    self.orderRecipbackView.layer.masksToBounds = true;
    [self.view addSubview:self.orderRecipbackView];
    [self.orderRecipbackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderbackView.mas_bottom).offset(10);
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.height.mas_equalTo(110 * scaleH);
    }];
    
    self.orderRecipView = [RecipInfoView new];
    [self.orderRecipbackView addSubview:self.orderRecipView];
    [self.orderRecipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.orderRuleView = [RentRuleView new];
    [self.view addSubview:self.orderRuleView];
    [self.orderRuleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.orderRecipbackView.mas_bottom).offset(5 * scaleH);
        make.leading.trailing.mas_equalTo(0);
        make.height.mas_equalTo(197 * scaleH);
    }];
    
    UIView *bottomView = [UIView new];
    bottomView.backgroundColor = myRGBHex(0xFFFFFF);
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.mas_equalTo(0);
        make.height.mas_equalTo(98 * scaleH);
    }];
    
    UILabel *sumLabel = [[UILabel alloc] init];
    sumLabel.text = @"合计：";
    sumLabel.font = [UIFont pingFangSCRegularWithSize:13];
    sumLabel.textColor = myRGBHex(0x999999);
    [bottomView addSubview:sumLabel];
    [sumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(28);
        make.leading.mas_equalTo(16);
        make.height.mas_equalTo(17);
    }];
    
    UILabel *priceLabel = [[UILabel alloc] init];
    priceLabel.text = [NSString stringWithFormat:@"¥%.0f",(self.rentModel.rentPrice * self.rentModel.rentCount * self.rentModel.rentPeriod * 30 + 2000)];
    priceLabel.font = [UIFont pingFangSCRegularWithSize:14];
    priceLabel.textColor = myRGBHex(0xF51D1D);
    [bottomView addSubview:priceLabel];
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.leading.equalTo(sumLabel.mas_trailing).offset(3);
        make.height.mas_equalTo(20);
    }];
    self.priceLabel = priceLabel;
    
    
    self.tenantButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.tenantButton setTitle:@"立即租赁" forState:UIControlStateNormal];
    [self.tenantButton setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
    self.tenantButton.titleLabel.font = [UIFont pingFangSCMediumWithSize:16];
    [self.tenantButton addTarget:self action:@selector(tenantButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:self.tenantButton];
    [self.tenantButton.titleLabel sizeToFit];
    self.tenantButton.layer.cornerRadius = 22;
    self.tenantButton.layer.masksToBounds = true;
    self.tenantButton.backgroundColor = myRGBHex(0x121212);
    [self.tenantButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.trailing.mas_equalTo(-17);
        make.size.mas_equalTo(CGSizeMake(175 * scaleW, 44));
    }];
}

- (void)tenantButtonPressed:(UIButton *)sender {
    
    if (self.orderRecipView.nameTextField.text.length > 0 &&
        self.orderRecipView.phoneTextField.text.length > 0 &&
        self.orderRecipView.addressTextField.text.length > 0) {
        self.rentModel.rentConsignee = self.orderRecipView.nameTextField.text;
        self.rentModel.rentContactInfo = self.orderRecipView.phoneTextField.text;
        self.rentModel.rentReceAddress = self.orderRecipView.addressTextField.text;
        self.rentModel.rentBuy = true;
        self.rentModel.rentBuyId = [[NSString randomNumberStringWithLength:4] integerValue];
        HomeOrderReviewController *vc = [HomeOrderReviewController new];
        vc.rentModel = self.rentModel;
        vc.hidesBottomBarWhenPushed = true;
        [self.navigationController pushViewController:vc animated:true];
        
        [[RentManager sharedManager] updateRentModel:self.rentModel];
        [[RentManager sharedManager] saveRentDataToFile];
        
        [RentModelManager saveRentModels:@[self.rentModel]];
        
    }else {
        [QLToast makeToast:@"请输入：收货人,联系方式,收货地址信息" toastType:QLToastTypeNormal toView:self.view];
    }
    
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
